import Cfg from './config.js';
import Net from './request.js';

//判断是否更新
function needUpdate(appVersion,olVersion){	
	appVersion = appVersion.replace(/[.]/g, "")
	olVersion = olVersion.replace(/[.]/g, "")
	if(olVersion>appVersion)return true;
	return false;
}

//判断是否执行更新
function chkUpgrade(appInfo) {		
	let ckUrl = Cfg.Setting.update_api;	
	let appVersion = appInfo.version.replace(/[.]/g, "") 
	let appName = appInfo.name
	let phoneOS = uni.getSystemInfoSync().platform
	Net._get(ckUrl,null,res=>{			
		let androidUdateFile = res.android //安卓更新文件
		let iosUdateFile = res.ios //ios更新文件	
		let updateFile = phoneOS == "android" ? androidUdateFile : iosUdateFile
		if (updateFile.appVersion.replace(/[.]/g, "") > appVersion) {
			if (updateFile.updateType) {					
				packageDownload(updateFile) //整包更新
			} else {								
				wgtDownload(updateFile) //wgt更新
			}
		}
	},true);		
}

//热更新
function wgtDownload(fileInfo) {
	const downloadTask = uni.downloadFile({
		url: fileInfo.wgtUdateUrl,
		success: (downloadResult) => {
			if (downloadResult.statusCode === 200) {				
				uni.showModal({
					title: "提示",
					content: '软件更新完成，是否重启应用?',
					success: res => {
						if (res.confirm) {														
							plus.runtime.install(downloadResult.tempFilePath, {
								force: false
							}, function() {								
								console.log('install success...');																									
								plus.runtime.restart();
							}, function(e) {
								console.error('install fail...');
							});
						}
					}
				});
			}
		}
	});
	downloadTask.onProgressUpdate((res) => {
		console.log('下载进度' + res.progress);
	})
}

//整包更新
function packageDownload(fileInfo) {
	uni.showModal({
		title: "提示",
		content: `检测到有最新版本${fileInfo.appVersion}，是否确认更新?`,
		success: function(res) {
			if (res.confirm) {
				plus.runtime.openURL(fileInfo.downloadUrl) //整包下载
			}
		}
	});
}


//整包更新 android处理 url:新包apk下载地址    目前使用
function android_all_update(url){
	//console.log('安卓开始整包更新处理，下载包');
	uni.showLoading({
		title:'正在下载最新安装包',
		mask:true
	})
	uni.downloadFile({
		url: url,   
		success: (downloadResult) => {  
			uni.hideLoading();
			if (downloadResult.statusCode === 200) {  
				uni.showLoading({
					title:'正在安装',
					mask:true
				})
				//console.log(downloadResult)
				plus.runtime.install(downloadResult.tempFilePath, {  
					force: false  
				}, function() {  
					uni.hideLoading();
					//console.log('安卓整包安装成功...');  
					//plus.runtime.restart();  //重新启动app资源
					uni.showToast({
						title:'安装成功，请重新进入',
						icon:'none',
						mask:true,
						duration:3500,
					})
					setTimeout(()=>{
						plus.runtime.quit();   //安装好后退出
					},3000)
				}, function(e) { 
					uni.hideLoading();
					//console.error('安卓整包安装失败...');  
					uni.showToast({
						title:'更新安装失败，请退出重试',
						icon:'none',
						mask:true,
						duration:3500,
					})
					setTimeout(()=>{
						plus.runtime.quit();
					},3000)
				});  
			}  
		},fail: () =>{
			//console.log('安卓整包下载失败')
			failFun();
		} 
	});  
}
module.exports = {
    needUpdate:needUpdate,   
	upgrade:chkUpgrade,
	android_all_update
}